package com.lagou.cl.processor;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
import org.apache.hadoop.hbase.coprocessor.ObserverContext;
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

public class WorkProcssor extends BaseRegionObserver {

	@Override
	public void preDelete(ObserverContext<RegionCoprocessorEnvironment> e, Delete delete, WALEdit edit, Durability durability) throws IOException {
		// 获取连接
		Configuration configuration = HBaseConfiguration.create();
		configuration.set("hbase.zookeeper.quorum", "linux111,linux112,linux113");
		configuration.set("hbase.zookeeper.property.clientPort", "2181");
		Connection connection = ConnectionFactory.createConnection(configuration);
		// 删除id=1的用户的朋友2
		Table table = connection.getTable(TableName.valueOf("user_relations"));
		Cell cell = delete.getFamilyCellMap().get(Bytes.toBytes("friends")).get(0);
		if (cell == null) {
			return;
		}

		// 开始检查friends列族内的操作情况
		byte[] qualifier = CellUtil.cloneQualifier(cell);
		//创建删除对象
		Delete ddDelete = new Delete(qualifier);
		ddDelete.addColumn(Bytes.toBytes("friends"), delete.getRow());
		table.delete(ddDelete);
	}
}
